package com.jianglin.state2.controller;

import com.jianglin.state2.core.ResponsePack;
import com.jianglin.state2.dao.entity.Employee;
import com.jianglin.state2.dao.entity.Team;
import com.jianglin.state2.model.params.TeamAddParams;
import com.jianglin.state2.model.params.TeamUpdateParams;
import com.jianglin.state2.service.EmployeeService;
import com.jianglin.state2.service.TeamService;
import com.jianglin.state2.util.SqlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponsePack
@RequestMapping("/team")
@Api(tags = {"团队"})
public class TeamController {
    private final TeamService teamService;

    @Autowired
    public TeamController(TeamService teamService) {
        this.teamService = teamService;
    }

    @GetMapping("/all")
    @ApiOperation("获得所有团队信息")
    public List<Team> getAll() {
        return teamService.list();
    }

    @GetMapping("/count/{teamId}")
    @ApiOperation("根据团队ID获取团队成员个数")
    public Integer getTeamNumById(@ApiParam("团队ID") @PathVariable Integer teamId){
        return teamService.listByMap(SqlUtil.getColumnMap("teamId",teamId)).size();
    }

    @GetMapping("/teamId/{teamId}")
    @ApiOperation("根据团队信息编号查找团队")
    public List<Team> getTeamByForm(@ApiParam("团队编号") @PathVariable Integer teamId){
        return teamService.listByMap(SqlUtil.getColumnMap("teamId",teamId));
    }

    @GetMapping("/number")
    @ApiOperation("获取团队数")
    public Integer getNumberByTeam()
    {
        return teamService.selectCountByTeam().size();
    }

    @PostMapping("/add")
    @ApiOperation("添加团队")
    public boolean saveDesk(@Validated @RequestBody TeamAddParams teamAddParams) {
        Team team = teamAddParams.convertOut();
        return teamService.save(team);
    }

    @PutMapping("/{teamMessage}")
    @ApiOperation("更新团队信息")
    public boolean updateEmployee(@ApiParam("团队信息编号") @PathVariable Integer teamMessage,
                                  @Validated @RequestBody TeamUpdateParams teamUpdateParams){
        Team team =teamUpdateParams.convertOut();
        team.setTeamMessage(teamMessage);
        return teamService.updateById(team);
    }

    @DeleteMapping("/{teamMessage}")
    @ApiOperation("删除团队")
    public boolean deleteTeam(@ApiParam("团队信息编号") @PathVariable Integer teamMessage) {
        return teamService.removeById(teamMessage);
    }
}